import os

import PbrtRenderUtils

from Katana import GeoAPI, RenderingAPI, ScenegraphAttr, OCIO


class PbrtPlugin(RenderingAPI.RenderPlugins.RendererPluginBase):
    def getRendererName():
        return "pbrt"
    
    def getRendererVersion(self, renderSettings):
        return "Pbrt_v2"

    def getRenderNodeObject(self, node, time, hotRender):
        return None

    def buildRenderCommand(self, producer, renderSettings, nodeName, renderTime,
                           sampleRate, ROI, mainSequenceId, sequenceID, useID, commonChildEnv):
        return PbrtRenderUtils.buildRenderCommand(
            producer, renderSettings, nodeName, renderTime, mainSequenceId, 
            sequenceID, useID, commonChildEnv)

    # list of available output types
    def getRenderOutputTypes(self):
        return (RenderingAPI.RenderOutputs.Type.Color,
                RenderingAPI.RenderOutputs.Type.ForceNone)

    def getOutputChannels(self, outputType=None):
        outputChannels = {}    
        if outputType is None or outputType == RenderingAPI.RenderOutputs.Type.Color:
            outputChannels = ['rgba']
        return outputChannels

    def getRendererSettingsAttr(self, outputType, producer=None, producerBaseName=None):
        attr, hints = RenderingAPI.RenderPlugins.RendererPluginBase.getRendererSettingsAttr(
            self, outputType, producer, producerBaseName)

        gb = GeoAPI.Util.GroupBuilder()
        if attr:
            gb.update(attr)
            hints = hints.copy()
        else:
            hints = {}

        #TODO: test value to set the pbrt scene to render
        gb.set('scene', ScenegraphAttr.Attr('StringAttr', ['/home/pbrt-katana/pbrt-scenes/bunny.pbrt']))
        hints['scene'] = {'widget':'fileInput'}



        # add in pbrt channel options hints
        if outputType == RenderingAPI.RenderOutputs.Type.Color:
            outputChannels = self.getOutputChannels(outputType)

            if producer:
                outputChannelsAttr = producer.getDelimitedLocalAttribute('pbrtGlobalStatements.outputChannels')
                if outputChannelsAttr:
                    outputChannelNames = outputChannelsAttr.childNames()
                    for name in outputChannelNames:
                        if name not in outputChannels:
                            outputChannels.append(name)
            hints['channel'] = {
                'widget': 'popup',
                'options': outputChannels
            }

        return gb.build(), hints

PluginRegistry = [
    ("RendererPlugin", 1, "pbrt", PbrtPlugin),
    ("RendererInfoPlugin", 1, "pbrt", "PbrtRendererInfo"),
]
